package com.canbot.u05.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.canbot.u05.R;
import com.canbot.u05.control.Cmd;
import com.canbot.u05.control.RobotMgr;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.view.RockerView;

/**
 * 手持pad  手动遥控界面
 */
public class HandleTelecontrolActivity extends BaseActivityNoTitle {

        // 360°平分4份的边缘角度(旋转45度)
        private static final double ANGLE_ROTATE45_4D_OF_0P = 45;

        private static final double ANGLE_ROTATE45_4D_OF_1P = 135;

        private static final double ANGLE_ROTATE45_4D_OF_2P = 225;

        private static final double ANGLE_ROTATE45_4D_OF_3P = 315;

        // 360°平分4份的边缘角度
        private static final double ANGLE_4D_OF_0P = 0;

        private static final double ANGLE_4D_OF_1P = 90;

        private static final double ANGLE_4D_OF_2P = 180;

        private static final double ANGLE_4D_OF_3P = 270;

        // 360°分四份，左右120°，上下60°
        private static final double ANGLE_4D_LRQ_0P = 45;

        private static final double ANGLE_4D_LRQ_1P = 135;

        private static final double ANGLE_4D_LRQ_2P = 225;

        private static final double ANGLE_4D_LRQ_3P = 315;

        private com.canbot.u05.view.RockerView rockerViewright;

        private com.canbot.u05.view.RockerView rockerViewleft;

        private int cmd;

        private Button mBack;

        private static final int START = 1;

        private static final int END = 2;

        private Handler mHandler = new Handler() {
                @Override
                public void handleMessage(Message message) {

                        switch (message.what) {
                                case START://底盘动作执行 前后左右
                                        Logger.e("HandleTelecontrolActivi", "处理消息" + cmd);
                                        RobotMgr.getInstance().keyControl(cmd);
                                        mHandler.removeMessages(START);
                                        mHandler.sendEmptyMessageDelayed(START, 500);
                                        break;
                                case END://底盘动作停止执行
                                        cmd = Cmd.Key_tingzhi;
                                        RobotMgr.getInstance().keyControl(cmd);
                                        break;
                                default:
                                        break;
                        }
                }
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_handle_telecontrol);
                initView();
                initEvent();

        }

        @Override
        protected void onPause() {
                super.onPause();
                mHandler.removeCallbacksAndMessages(null);
        }

        private void initEvent() {
                //左边腰杆
                if (rockerViewleft != null) {
                        rockerViewleft.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_STATE_CHANGE);
                        rockerViewleft.setOnShakeListener(RockerView.DirectionMode.DIRECTION_4_ROTATE_45, new RockerView.OnShakeListener() {
                                @Override
                                public void onStart() {
                                        //mLogLeft.setText(null);
                                        Logger.d("HandleTelecontrolActivi", "開始");
                                }

                                @Override
                                public void direction(RockerView.Direction direction, double angle) {
                                        Logger.e("HandleTelecontrolActivi", "左边腰杆--direction");
//                                        //划分象限范围 并返回不同方向需要发送的命
                                        cmd = divideRange(angle);
                                        mHandler.removeMessages(START);
                                        mHandler.sendEmptyMessageDelayed(START, 500);


                                }

                                @Override
                                public void onFinish() {
                                        mHandler.removeMessages(END);
                                        mHandler.removeMessages(START);
                                        mHandler.sendEmptyMessage(END);
                                        Logger.d("HandleTelecontrolActivi", "結束");

                                }
                        });

                }
                //右边摇杆
                if (rockerViewright != null) {
                        rockerViewright.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_STATE_CHANGE);
                        rockerViewright.setOnShakeListener(RockerView.DirectionMode.DIRECTION_4_ROTATE_45, new RockerView.OnShakeListener() {
                                @Override
                                public void onStart() {
                                        Logger.d("HandleTelecontrolActivi", "開始");
                                }

                                @Override
                                public void direction(RockerView.Direction direction, double angle) {
                                        angle = transformAngle(angle);//角度转换

                                        Logger.d("HandleTelecontrolActivi", "角度 " + angle);
                                        switch (direction) {
                                                case DIRECTION_LEFT:
                                                        RobotMgr.getInstance().headControl(Cmd.Joint_Hor, (short) (angle));
                                                        break;
                                                case DIRECTION_RIGHT:
                                                        RobotMgr.getInstance().headControl(Cmd.Joint_Hor, (short) (-angle));
                                                        break;
                                                case DIRECTION_UP:
                                                        RobotMgr.getInstance().headControl(Cmd.Joint_Ver, (short) (-angle));
                                                        break;
                                                case DIRECTION_DOWN:
                                                        RobotMgr.getInstance().headControl(Cmd.Joint_Ver, (short) (angle));
                                                        break;
                                                default:
                                                        break;
                                        }

                                }

                                @Override
                                public void onFinish() {
                                        Logger.d("HandleTelecontrolActivi", "結束");
                                }
                        });

                }

                mBack.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                finish();
                        }
                });
//                mDance.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                                finish();
//                                Intent intent=new Intent(HandleTelecontrolActivity.this,ActionDanceActivity.class);
//                                startActivity(intent);
//                        }
//                });
        }

        private void initView() {
                this.rockerViewleft = (RockerView) findViewById(R.id.rockerView_left);
                this.rockerViewright = (RockerView) findViewById(R.id.rockerView_right);
                mBack = (Button) findViewById(R.id.btn_back_handle_telecontrol);
//                mDance = (Button) findViewById(R.id.btn_action_handle);
        }


        /**
         * 将每个象限的角度都转换成0-90°.
         *
         * @param angle
         * @return
         */
        private double transformAngle(double angle) {
                if (angle >= ANGLE_ROTATE45_4D_OF_3P) {
                        return angle - ANGLE_4D_OF_3P;
                }
                else if ((angle >= ANGLE_ROTATE45_4D_OF_0P) && (angle < ANGLE_ROTATE45_4D_OF_1P)) {
                        return angle - ANGLE_ROTATE45_4D_OF_0P;
                }
                else if ((angle >= ANGLE_ROTATE45_4D_OF_1P) && (angle < ANGLE_ROTATE45_4D_OF_2P)) {
                        return angle - ANGLE_ROTATE45_4D_OF_1P;
                }
                else if ((angle >= ANGLE_ROTATE45_4D_OF_2P) && (angle < ANGLE_ROTATE45_4D_OF_3P)) {
                        return angle - ANGLE_ROTATE45_4D_OF_2P;
                }

                return angle;

        }

        private int divideRange(double angle) {
                if ((angle >= ANGLE_4D_LRQ_3P) || (angle <= ANGLE_4D_LRQ_0P)) {//右
                        return cmd = Cmd.Key_youzhuan;//右转
                }
                else if ((angle > ANGLE_4D_LRQ_0P) && (angle < ANGLE_4D_LRQ_1P)) {//下
                        return cmd = Cmd.Key_houtui;//后退
                }
                else if ((angle >= ANGLE_4D_LRQ_1P) && (angle <= ANGLE_4D_LRQ_2P)) {//左
                        return cmd = Cmd.Key_zuozhuan;//左转
                }
                else if ((angle > ANGLE_4D_LRQ_2P) && (angle < ANGLE_4D_LRQ_3P)) {//上
                        return cmd = Cmd.Key_qianjin;//前进
                }
                return cmd;
        }


}
